package com.dhecp.project.business.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.business.domain.IndexImage;
import com.dhecp.project.business.mapper.IndexImageMapper;
import com.dhecp.project.business.service.IndexImageService;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 商品描述 服务层
 * @author DH-Xxj
 * @version 2022年7月25日
 */
@Service
public class IndexImageServiceImpl extends BaseModuleServiceImpl implements IndexImageService {

	@Autowired
	private IndexImageMapper businessMapper;
	
	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;
	

	
	/**
	 * 查询
	 * @param params
	 * @return List - IndexImage
	 * @throws Exception
	 * @author DH-Xxj
	 */
	@Override
    public List<IndexImage> queryList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        PageUtils.startPage();

        params.put("orderBy", "t1.iid asc");
        return businessMapper.select(params);
    }

	/**
	 * 查询
	 * @param params
	 * @return List - IndexImage
	 * @throws Exception
	 * @author DH-Xxj
	 */
	@Override
    public IndexImage findById(Map<String, Object> params) throws Exception {
        return businessMapper.findById(params);
    }
	

	
	/**
	 * 保存
	 * @param moduleTable 必要参数：功能唯一标识
	 * @param infoMap 必要参数：非网格字段（包括头部、底部所有显示和隐藏的字段）
	 * @param infoItems 必要参数：对应实体类的json格式字符串
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
	@SuppressWarnings("unchecked")
	@Override
    public AjaxResult saveData(Map<String, Object> params) {

		IndexImage indexImage = JSON.parseObject(params.get("infoItems").toString(), IndexImage.class);

		if (indexImage.getGuid() != null && !indexImage.getGuid().equals("")){
			businessMapper.update(indexImage);
		}else{
			indexImage.setGuid(RandomValueUtils.getGUID());
			indexImage.setCreateTime(new Date());
			businessMapper.insert(indexImage);
		}

		return AjaxResult.success(MessageUtils.message("module.success.issuccess"), indexImage.getGuid());

	}
	
	/**
	 * 删除
	 * @param infoItems 必要参数：对应实体类的json格式字符串
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
	@Override
    public AjaxResult deleteData(Map<String, Object> params) {
        List<IndexImage> businessDataList = JSON.parseArray(params.get("infoItems").toString(), IndexImage.class);
        if (businessDataList == null || businessDataList.size() == 0) {
            return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
        }
        for (IndexImage businessData : businessDataList) {
        	businessMapper.delete(businessData);
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }
	


}
